package ViewPackage;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

import delta.*;

public class GameController extends Thread{
	//idokezeleshez konstans
	private final int ticksMax=3000;
	
	boolean running=true;
	//ez is megkapja, hogy tudjon ra tenni objektum viewet
	GameView gv;
	
	Mordor world;
	
	//Veletlenszam generator
	Random rnd=new Random();
	
	//idokezeleshez valtozok
	int ticks=0;
	int rate=75;
	
	boolean hasAlive = false;
	
	//Ebben majd a GameView -en kivul at kellene adni egy mordort
	//Most tesztelesi celbol csak fieldet kap
	public GameController(GameView gameView,Mordor world){
		gv=gameView;
		
		this.world=world;
	}
	
	public void run(){		
		long beginTime=System.currentTimeMillis();
		double delta = 0;
		
//		for(int i=0;i<20;i++){
//		
//			if(Main.map[i][Main.map[i].length-1]!=0)Main.rekursiveAddRoad(fields,i,19,-3,-3,1);
//		}
//		for(int i=0;i<fields.length;i++){
//			for(int j=0;j<fields[i].length;j++){
//				if(fields[i][j].getDistance()!=-1){
//					Road rd=(Road)fields[i][j];
//					rd.setDistance(Main.map[i][j]);
//				}
//			}
//		}
		
		while(running){
			long now=System.currentTimeMillis();
			
			delta+=(now-(ticks*50)-beginTime)/50.0;
			
			if(delta>=1){
				if(ticks<=ticksMax){
					if(ticks % rate == 0) gameTimerTick();
					if(ticks % 200 == 0) fogTick();
					if(ticks==1000) rate=50;
					if(ticks==2000) rate=25;
				}
				else{
					for(int i=0;i<20;i++){
						for(int j=0;j<20;j++){
							if(world.getFields()[i][j].getDistance()!=-1){
								ArrayList<IFieldable> ob = world.getFields()[i][j].getObjects();
								for(int k=0;k<ob.size();k++){
									if(ob.get(k).action(null)){
										hasAlive = true;
									}
								}
							}
						}
					}
					if(hasAlive == false){
						Player.setIsChampOfWorld(true);
					}
					hasAlive = false;
				}
				delta--;
				ticks++;
				if(Player.getIsEndOfWorld()==true) running=false;
				if(Player.getIsChampOfWorld()==true) running=false;
				//A mordor update-elese
				world.updateWorld();
				
			}
		}
	}
	
	//Ebben a fuggvenyben update -elodik a jatek
	public void gameTimerTick(){
		
		//egy veletlenszeru kezdout lekerese
		Road rd=world.getRandomStartRoad();
			
		int number=rnd.nextInt(4);
			
			
		if(number==0){
			IFieldable dwarf=new Dwarf(rd);
			gv.addIFieldableView(new DwarfView(gv, dwarf));
		}
		else if(number==1){
			IFieldable elf=new Elf(rd);
			gv.addIFieldableView(new ElfView(gv, elf));
		}
		else if(number==2){
			IFieldable hobbit=new Hobbit(rd);
			gv.addIFieldableView(new HobbitView(gv, hobbit));
		}
		else if(number==3){
			IFieldable human=new Human(rd);
			gv.addIFieldableView(new HumanView(gv, human));
		}
			
	}
	public void fogTick(){
		//kod lerakas
		Field[][] fields=world.getFields();
		if(fields!=null){
			int i=rnd.nextInt(fields.length);
			int j=rnd.nextInt(fields[i].length);	
			Fog fog=new Fog(fields[i][j]);
			gv.addIFieldableView(new FogView(gv,fog));
		}
	}

}
